﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuthoSYS.EF;

namespace AuthoSYS.Web.Controllers
{
    public class ApplicationController : Controller
    {
        private AuthoDBEntities db = new AuthoDBEntities();

        //
        // GET: /Application/

        public ActionResult Index(string serverId)
        {
            IEnumerable<Application> applications = null;
            if (!string.IsNullOrEmpty(serverId))
            {
                applications = db.Applications.Include("AppServer").Where(a => a.ServerId == serverId);
            }
            else
            {
                applications = db.Applications.Include("AppServer");
            }

            return View(applications.ToList());
        }

        //
        // GET: /Application/Details/5

        public ActionResult Details(string id = null)
        {
            Application application = db.Applications.Single(a => a.Id == id);
            if (application == null)
            {
                return HttpNotFound();
            }
            return View(application);
        }

        //
        // GET: /Application/Create

        public ActionResult Create()
        {
            ViewBag.ServerId = new SelectList(db.AppServers, "Id", "ServerName");
            return View();
        }

        //
        // POST: /Application/Create

        [HttpPost]
        public ActionResult Create(Application app)
        {
            if (ModelState.IsValid)
            {
                app.Id = Guid.NewGuid().ToString();
                app.ModifiedBy = string.Empty;
                app.ModifiedOn = DateTime.Now;
                app.CreatedBy = string.Empty;
                app.CreatedOn = DateTime.Now;
                db.Applications.AddObject(app);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ServerId = new SelectList(db.AppServers, "Id", "ServerName", app.ServerId);
            return View(app);
        }

        //
        // GET: /Application/Edit/5

        public ActionResult Edit(string id = null)
        {
            Application application = db.Applications.Single(a => a.Id == id);
            if (application == null)
            {
                return HttpNotFound();
            }
            ViewBag.ServerId = new SelectList(db.AppServers, "Id", "ServerName", application.ServerId);
            return View(application);
        }

        //
        // POST: /Application/Edit/5

        [HttpPost]
        public ActionResult Edit(Application app)
        {
            if (ModelState.IsValid)
            {
                db.Applications.Attach(app);
                app.ModifiedBy = string.Empty;
                app.ModifiedOn = DateTime.Now;
                db.ObjectStateManager.ChangeObjectState(app, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ServerId = new SelectList(db.AppServers, "Id", "ServerName", app.ServerId);
            return View(app);
        }

        //
        // GET: /Application/Delete/5

        public ActionResult Delete(string id = null)
        {
            Application application = db.Applications.Single(a => a.Id == id);
            if (application == null)
            {
                return HttpNotFound();
            }
            return View(application);
        }

        //
        // POST: /Application/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Application application = db.Applications.Single(a => a.Id == id);
            db.Applications.DeleteObject(application);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}